<?php
	class GenericSDBModel extends SDBModel
	{
		public function __construct()
		{
			$config = Zend_Registry::get('config');
			parent::__construct();
			$this->setDomain($config->domain->name->test);
		}

		public function __destruct()
		{
			parent::__destruct();
		}
	}

	class GenericSDBManager extends SDBManager
	{
		public function __construct()
		{
			$config = Zend_Registry::get('config');
			parent::__construct($config->domain->name->test, 'GenericSDBModel');
		}

		public function __destruct()
		{
			parent::__destruct();
		}
	}
	
	class SDBManagerTest extends PHPUnit_Framework_TestCase
	{
		protected $_ids = array();
		protected $_sdb_mgr;
		protected $_config;

		protected function setUp() 
		{ 
			$this->_sdb_mgr = new GenericSDBManager();
			$this->_config = Zend_Registry::get('config');

			$attributes = array(
				array('name' => 'unit'),
				array('name' => 'test')
			);
			
			foreach($attributes as $current_attributes)
			{
				$gen_obj = new GenericSDBModel();
				$gen_obj->setAttributes($current_attributes);
				$gen_obj->save();
				$this->_ids[] = $gen_obj->getId();
			}
		}

		protected function tearDown() 
		{ 
			foreach ($this->_ids as $id)
			{
				$gen_obj = new GenericSDBModel();
				$gen_obj->load($id);
				$gen_obj->delete();
			}
		}
		
		public function testQuery()
		{
			$objs = $this->_sdb_mgr->query("['name' = 'unit']");
			$this->assertTrue(sizeof($objs) > 0);
			$this->assertTrue(sizeof($objs) == 1);
			$this->assertTrue(get_class($objs[0]) == 'GenericSDBModel');
			$this->assertTrue($objs[0]->getAttribute('name') == 'unit');
		}

		public function testQueryMaxItems()
		{
			$objs = $this->_sdb_mgr->query('', 1);
			$this->assertTrue(sizeof($objs) == 1);

			$objs = $this->_sdb_mgr->query('');
			$this->assertTrue(sizeof($objs) > 1);
		}
		
		public function testGetFieldByValue()
		{
			$objs = $this->_sdb_mgr->getByFieldValue('name', 'unit');
			$this->assertTrue(sizeof($objs) > 0);
			$this->assertTrue(sizeof($objs) == 1);
			$this->assertTrue(get_class($objs[0]) == 'GenericSDBModel');
			$this->assertTrue($objs[0]->getAttribute('name') == 'unit');
		}
		
		public function testIsUniqueFieldValue()
		{
			$result = $this->_sdb_mgr->isUniqueFieldValue('name', 'unit');
			$this->assertFalse($result);

			$result = $this->_sdb_mgr->isUniqueFieldValue('name', 'unit_test');
			$this->assertTrue($result);
		}
		
		public function testGetDomain()
		{
			$this->assertTrue($this->_sdb_mgr->getDomain() ==
				$this->_config->domain->name->test);
		}

		public function testGetObjectType()
		{
			$this->assertTrue($this->_sdb_mgr->getObjectType() == 'GenericSDBModel');
		}
	}
